package com.link.tools.wxpay.mini;

import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.link.tools.SpringBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.stream.Collectors;

@Slf4j
public class WXMiniPay {

    // 创建支付
    public static void createOrderV3() throws JSONException, WxPayException {

        // 微信支付
        WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
        request.setOutTradeNo("业务系统订单id");
        request.setDescription("描述");

        JSONObject attach = new JSONObject();
        attach.put("payRecords", "id");// 自定义扩展字段
        request.setAttach(attach.toString());// 附加数据
        request.setNotifyUrl(SpringBean.bean.wxMiniConfig.getNotifyUrl());// 回调通知
        request.setGoodsTag("");// 订单优惠标记
        request.setSupportFapiao(false);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setCurrency("CNY");
//            amount.setTotal(amountPay);
        amount.setTotal(1);
        request.setAmount(amount);
        WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
        // payer.setOpenid(user.getOpenId());
        payer.setOpenid("ocQpn5Pcv9j8PM3GlhtsMsXZuJS8");
        request.setPayer(payer);
        WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = SpringBean.bean.wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);

        log.info("createOrderV3:{}", jsapiResult);
    }
}
